package cn.hzc.fortess.core.common.exception;

import cn.hzc.fortess.core.common.BizErrorEnum;
import cn.hzc.fortess.core.common.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.io.IOException;

@ControllerAdvice
@Order(200)
public class DefaultExceptionHandler {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(ServiceException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ResponseData notFount(ServiceException e) {
        log.info("业务异常:", e);
        return ResponseData.error(e.getCode(), e.getErrorMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ResponseData serverError(Exception e) {
        if(!(e instanceof IOException)) {
            log.error("运行时异常:", e);
        }
        return ResponseData.error(BizErrorEnum.SERVER_ERROR.getCode(), BizErrorEnum.SERVER_ERROR.getMessage());
    }
}